package com.pe.pgn.clubpgn.webapp.controller.asignacionarticulo;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpbAlmacen;
import com.pe.pgn.clubpgn.domain.ClpbCategoria;
import com.pe.pgn.clubpgn.domain.ClpbDocumentoMovArticulo;
import com.pe.pgn.clubpgn.domain.ClpdArticulo;
import com.pe.pgn.clubpgn.domain.ClpmCatalogo;
import com.pe.pgn.clubpgn.domain.beans.BNAsignacionArticuloCatalogoDetalle;
import com.pe.pgn.clubpgn.domain.beans.BNDocumentoMovArticuloDet;
import com.pe.pgn.clubpgn.domain.beans.BNPremioCatalogo;
import com.pe.pgn.clubpgn.service.AlmacenService;
import com.pe.pgn.clubpgn.service.AsignacionArticuloService;
import com.pe.pgn.clubpgn.service.CatalogoService;
import com.pe.pgn.clubpgn.service.CategoriaService;
import com.pe.pgn.clubpgn.service.DocumentoMovArticuloDetService;
import com.pe.pgn.clubpgn.service.PremioCatalogoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;


@Controller
@RequestMapping("/asignacionarticulo/**")
public class AsignacionArticuloController extends BaseFormController {

	
	@Autowired
	private AlmacenService almacenService;
	
	@Autowired
	private AsignacionArticuloService docAsigArticuloService;	

	@Autowired
	private DocumentoMovArticuloDetService documentoDetService;	
	
	@Autowired
	private CatalogoService catalogoService;
	
	@Autowired
	private CategoriaService categoriaService;	
	
	@Autowired
	private PremioCatalogoService premiocatalogoService;	
	
	
	private static final String ES_EDITABLE = "esEditable";
	
	@RequestMapping(value = "/asignacionarticulolist.html")
    public void listarCatalogos(Model model) {
		List<ClpbDocumentoMovArticulo> asignacionarticulos = docAsigArticuloService.obtenerDocumentoAsignacionArticulos();
		List<ClpbAlmacen> almacenes = almacenService.obtenerAlmacenes(Boolean.FALSE);
		model.addAttribute("almacenes", almacenes);
		model.addAttribute("asignacionarticulo", asignacionarticulos);
    }
	
	
	@RequestMapping(value = "/asignacionarticuloform.html", method = RequestMethod.GET)
	public @ModelAttribute("asignacionarticulo") ClpbDocumentoMovArticulo verAsignacionArticulo(
				@RequestParam(value = "id", required = false) Long id, Model model) {

		List<ClpbAlmacen> almacenes = almacenService.obtenerAlmacenes(Boolean.FALSE);
		List<ClpmCatalogo> catalogos = catalogoService.obtenerListaCatalogosVigente();
		
		model.addAttribute("almacenes", almacenes);
		model.addAttribute("catalogos", catalogos);
		
		ClpbDocumentoMovArticulo documento = new ClpbDocumentoMovArticulo();
		if (id != null) {			
			
			documento = docAsigArticuloService.obtenerDocumentoAsignacionArticulo(id);
			List<BNDocumentoMovArticuloDet> detalleDocumento = documentoDetService.obtenerDocumentoMovimientos(documento);
			
			model.addAttribute("movimientos", detalleDocumento);	
			model.addAttribute("asignacionarticulo",documento);
			model.addAttribute(ES_EDITABLE, Boolean.FALSE);			
			return documento;
		}
		model.addAttribute(ES_EDITABLE, Boolean.TRUE);	
		return documento;
	}
	
	@RequestMapping(value = "/asignacionarticuloform.html", method = RequestMethod.POST)
	public String guardarAsignacionArticulo(@ModelAttribute("asignacionarticulo") ClpbDocumentoMovArticulo documento,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {
		
		try {
			
			//Validando Stok disponible a devolver
			for (Iterator<BNAsignacionArticuloCatalogoDetalle> iter = documento.getDetalle().iterator(); iter.hasNext(); ) {
				BNAsignacionArticuloCatalogoDetalle articulo = iter.next();
				
				if (articulo.getCodigoArticulo() != null){
					ClpdArticulo articuloBean = docAsigArticuloService.obtenerArticulo(articulo.getCodigoArticulo());
					if (articulo.getStockDisponibleAsignado().doubleValue() > articuloBean.getVaStockDisponible().doubleValue()) {
						List<ClpbAlmacen> almacenes = almacenService.obtenerAlmacenes(Boolean.FALSE);
						List<ClpmCatalogo> catalogos = catalogoService.obtenerListaCatalogos();
						model.addAttribute("almacenes", almacenes);
						model.addAttribute("catalogos", catalogos);					
						saveError(request, getText("asignacionarticuloForm.error.stockDisponibleAsignado", articuloBean.getDeArticulo(), request.getLocale()));
						return "/asignacionarticulo/asignacionarticuloform";
					}
				}				
			}			
			
			docAsigArticuloService.guardarAsignacionArticulo(documento);
			saveMessage(request, getText("asignacionarticuloform.added", request.getLocale()));
			return "redirect:asignacionarticuloform.html?id="+documento.getId();
			
		} catch (Exception e) {			
			List<ClpbAlmacen> almacenes = almacenService.obtenerAlmacenes(Boolean.FALSE);
			List<ClpmCatalogo> catalogos = catalogoService.obtenerListaCatalogos();
			model.addAttribute("almacenes", almacenes);
			model.addAttribute("catalogos", catalogos);
			saveError(request, getText("errors.general", request.getLocale()));
			e.printStackTrace();
			return "/asignacionarticulo/asignacionarticuloform";
		}
		
	}	
		
	
	@RequestMapping(value = "/articuloForAsignacionArticulo.html", method = RequestMethod.GET)
	public String  listarArticulos(
			@RequestParam(value = "coCatalogo", required = true) Long coCatalogo,
			@RequestParam(value = "coAlmacen", required = false) Long coAlmacen,
			Model model) {
			List<BNPremioCatalogo> premios = premiocatalogoService.buscarPremios("", "", new Long(0), coCatalogo, coAlmacen);
			List<ClpbCategoria> categorias = categoriaService.obtenerCategorias();
			model.addAttribute("listapremios", premios);
			model.addAttribute("categorias", categorias);
		return "/wizard/articuloasignacionarticulowizardform";
	}	
}
